<?php
/**
 * @author wangkangchao
 */
namespace app\admin\controller\business;

use app\common\controller\Backend;
use think\Db;
use think\Exception;
use app\admin\model\business\BusinessBanner as ModelBusinessBanner;
use app\admin\validate\business\BusinessBanner as ValidateBusinessBanner;

/**
 * [自建]商务合作banner表控制器
 */
class BusinessBanner extends Backend
{
    protected $model = null;
    //当前登录管理员所有子组别
    protected $childrenGroupIds = [];
    //当前组别列表数据
    protected $groupdata = [];
    //无需要权限判断的方法
    protected $noNeedRight = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new ModelBusinessBanner();
        $this->validate = new ValidateBusinessBanner();
      
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->where('status','<>','deleted')
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->where('status','<>','deleted')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $validate_result = $this->validate->check($params);
                if($validate_result){
                    $result = $this->model->save($params);
                    if ($result === false) {
                        $this->error($this->model->getError());
                    }
                    $this->success();
                }
                $this->error($this->validate->getError());
            }
            $this->error();
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $validate_result = $this->validate->check($params);
                if($validate_result){
                    $result = $row->save($params);
                    if ($result === false) {
                        $this->error($row->getError());
                    }
                    $this->success();
                }
                $this->error($this->validate -> getError());
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $delIds = explode(',', $ids);
            //$count = $this->model->where('id', 'in', $delIds)->delete();
            //软删除
            $count = $this->model->where('id', 'in', $delIds)->update(['status'=>'deleted','deletetime'=>time()]);
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }

}

    

    

